package com.example.pixelyear.utils

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.example.pixelyear.R
import kotlinx.android.synthetic.main.empty_layout.view.*

object LayoutUtils {
    /**
     * 缺省图显示Activity
     *@ids 一定要使用intArrayOf()
     */
    fun goneLayout(
        activity: Activity,
        ids: IntArray,
        text: String = "没有了哦，小宝贝～",
        res: Int = R.mipmap.no,
        state: Int = View.GONE
    ) {
        val viewGroup: ViewGroup = activity.findViewById(android.R.id.content)
        if (viewGroup.tag != null) return
        val relative: RelativeLayout =
            LayoutInflater.from(activity).inflate(R.layout.empty_layout, null, false) as RelativeLayout
        viewGroup.removeView(relative)
        viewGroup.addView(relative)
        if (res == 0) {
            relative.EmptyLayout_img.visibility = View.GONE
        } else {
            relative.EmptyLayout_img.visibility = View.VISIBLE
            Glide.with(activity).load(res).into(relative.EmptyLayout_img)
        }
        relative.EmptyLayout_text.also {
            if (text.isEmpty())
                it.visibility = state
            else {
                it.visibility = View.VISIBLE
                it.text = text
            }
        }
        ids.forEach {
            activity.findViewById<View>(it).visibility = View.GONE
        }
        viewGroup.tag = relative
    }

    /**
     * 缺省图显示 Fragment
     * 警告，外部不要使用RelativeLayout包裹，会出现堆叠问题,
     */
    fun goneLayout(activity: Activity, view: View, ids: IntArray, text: String, res: Int = R.mipmap.no) {
        if (view is ViewGroup) {
            val relative: RelativeLayout =
                LayoutInflater.from(activity).inflate(R.layout.empty_layout, view, false) as RelativeLayout
            if (view.tag != null) return
            view.removeView(relative)
            view.addView(relative)
            layoutParamsMatchParent(relative)
            if (res == 0)
                relative.EmptyLayout_img.visibility = View.GONE
            else {
                relative.EmptyLayout_img.visibility = View.VISIBLE
                Glide.with(activity).load(res).into(relative.EmptyLayout_img)
            }
            relative.EmptyLayout_text.also {
                if (text.isEmpty())
                    it.visibility = View.GONE
                else {
                    it.visibility = View.VISIBLE
                    it.text = text
                }
            }
            ids.forEach {
                view.findViewById<View>(it).visibility = View.GONE
            }
            view.tag = relative
        }
    }

    /**
     * 缺省图移除Activity
     */
    fun visibleLayout(activity: Activity, ids: IntArray) {
        val viewGroup: ViewGroup = activity.findViewById(android.R.id.content)
        if (viewGroup.tag == null) {
            val relative: RelativeLayout =
                LayoutInflater.from(activity).inflate(R.layout.empty_layout, null, false) as RelativeLayout
            viewGroup.removeView(relative)
        } else {
            viewGroup.removeView(viewGroup.tag as View)
            viewGroup.tag = null
        }
        ids.forEach {
            activity.findViewById<View>(it).visibility = View.VISIBLE
        }
    }

    /**
     * 缺省图移除Fragment
     */
    fun visibleLayout(activity: Activity, view: View, ids: IntArray) {
        if (view is ViewGroup) {
            val relative: RelativeLayout =
                LayoutInflater.from(activity).inflate(R.layout.empty_layout, view, false) as RelativeLayout
            if (view.tag == null) {
                view.removeView(relative)
            } else {
                view.removeView(view.tag as View)
                view.tag = null
            }
            ids.forEach {
                view.findViewById<View>(it).visibility = View.VISIBLE
            }
        }
    }


    /**
     * 在Fragment中动态添加布局 布局会变成wrap_content，需要代码设置其Match_Panrent
     */
    private fun layoutParamsMatchParent(group: ViewGroup) {
        val layoutParams = group.layoutParams
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        group.layoutParams = layoutParams
    }

}
